<!--加入购物车和购买的计数组件-->
<script>
export default {
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    subCount () {
      this.$emit('input', this.value - 1)
    },
    addCount () {
      this.$emit('input', this.value + 1)
    },
    changeCount (e) {
      const num = +e.target.value
      if (isNaN(num) || num < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  }
}
</script>

<template>
  <div class="countBox">
    <button @click="subCount" class="left">-</button>
    <input class="count" @change="changeCount" type="text" :value="value">
    <button @click="addCount" class="right">+</button>
  </div>
</template>

<style scoped lang="less">
.countBox {
  display: flex;
  width: 110px;
  .left, .right {
    width: 30px;
    height: 30px;
    border: none;
    outline: none;
    background-color: #efefef;
  }
  .count {
    width: 40px;
    height: 30px;
    border: none;
    outline: none;
    margin: 0 5px;
    text-align: center;
    background-color: #efefef;
  }
}
</style>
